package com.wayhome.srpingbootmybatis.policy.controller;

import com.alibaba.excel.EasyExcel;
import com.wayhome.srpingbootmybatis.config.ApiResult;
import com.wayhome.srpingbootmybatis.config.CSPService;
import com.wayhome.srpingbootmybatis.exception.CustomerException;
import com.wayhome.srpingbootmybatis.policy.dto.InsertPolicyDocDTO;
import com.wayhome.srpingbootmybatis.policy.dto.PolicyDocDTO;
import com.wayhome.srpingbootmybatis.policy.dto.ReqPolicyDocDTO;
import com.wayhome.srpingbootmybatis.policy.dto.ReqPolicyDocFileDTO;
import com.wayhome.srpingbootmybatis.policy.service.PolicyDocService;
import com.wayhome.srpingbootmybatis.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/web/policyDoc")
public class PolicyDocController {

    @Resource
    private PolicyDocService policyDocService;

    @Resource
    private CSPService cspService;

    public static final String BUCKET = "policy-doc";

    @PostMapping(value = "/insertDoc")
    public ApiResult<Boolean> insertDoc(InsertPolicyDocDTO insertPolicyDocDTO) throws Exception {
        PolicyDocDTO policyDocDTO = new PolicyDocDTO();
        if (Objects.nonNull(insertPolicyDocDTO)) {
            BeanUtils.copyProperties(insertPolicyDocDTO, policyDocDTO);
            try {
                if (!StringUtils.isEmpty(insertPolicyDocDTO.getPubDateStr())) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date pubDate = simpleDateFormat.parse(insertPolicyDocDTO.getPubDateStr());
                    policyDocDTO.setPubDate(pubDate);
                }
            } catch (Exception e) {
              throw new CustomerException("发布日期格式错误，请按照2025-01-01这种格式传递");
            }
        }
        return policyDocService.insertPolicyDoc(policyDocDTO);
    }

    @PostMapping(value = "/queryList")
    public ApiResult<List<PolicyDocDTO>> queryList(@RequestBody ReqPolicyDocDTO reqPolicyDocDTO) throws Exception {
        return policyDocService.queryList(reqPolicyDocDTO);
    }

    @PostMapping(value = "/queryDocDetail")
    public ApiResult<PolicyDocDTO> queryDocDetail(@RequestBody ReqPolicyDocDTO reqPolicyDocDTO) throws Exception {
        return policyDocService.queryDocDetail(reqPolicyDocDTO);
    }

    @PostMapping(value = "/deleteDoc")
    public ApiResult<Boolean> deleteDoc(@RequestBody PolicyDocDTO policyDocDTO) throws Exception {
        return policyDocService.deleteDoc(policyDocDTO);
    }

    @PostMapping(value = "/updateDoc")
    public ApiResult<Boolean> updateDoc(InsertPolicyDocDTO update) throws Exception {
        PolicyDocDTO policyDocDTO = new PolicyDocDTO();
        if (Objects.nonNull(update)) {
            BeanUtils.copyProperties(update, policyDocDTO);
            try {
                if (!StringUtils.isEmpty(update.getPubDateStr())) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date pubDate = simpleDateFormat.parse(update.getPubDateStr());
                    policyDocDTO.setPubDate(pubDate);
                }
            } catch (Exception e) {
                throw new CustomerException("发布日期格式错误，请按照2025-01-01这种格式传递");
            }
        }
        return policyDocService.updateDoc(policyDocDTO);
    }

    @PostMapping(value = "/importPolicyDocFile")
    public void importPolicyDocFile(MultipartFile file) throws Exception {
        policyDocService.importDocFile(file);
    }

    @PostMapping(value = "/uploadDocFile")
    public ApiResult<Boolean> uploadDocFile(ReqPolicyDocFileDTO request) throws Exception {
        return policyDocService.uploadDocFile(request);
    }

    @PostMapping(value = "/downloadPolicyFile")
    public void downloadPolicyFile(@RequestBody ReqPolicyDocDTO reqPolicyDocDTO) throws Exception {
         policyDocService.downloadPolicyFile(reqPolicyDocDTO);
    }

    @PostMapping("/append-note")
    public void appendNote(HttpServletResponse response,
                           @RequestParam String note) throws Exception {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=with_note.xlsx");

        EasyExcel.write(response.getOutputStream())
                .withTemplate(new File("/Users/liangcai/Desktop/template.xlsx"))
                .sheet()
                .doWrite(() -> {
                    List<List<String>> notes = new ArrayList<>();
                    notes.add(Arrays.asList("说明：", note));
                    return notes;
                });
    }

    @PostMapping(value = "/exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws Exception {
        cspService.download(response, BUCKET,"政策明细登记表（终-5）.xlsx", "template","政策登记模版.xlsx");
    }


}
